<?php
namespace app\api\controller\v2\controller\pet;

use app\api\controller\v2\model\pet\Fosterage as Fo;
use think\Controller;
use app\api\controller\Api;
use think\Model;
use think\Db;
use think\Image;
use think\Request;
use JPush\Client as JPush;
use think\Response;
use app\api\controller\UnauthorizedException;
use think\Validate;


class Fosterage extends api
{
//    填写寄养表信息
    public function fosterage_form($user_id,$be_fosterage_user_id,$fosterage_name,$fosterage_phone,$fosterage_reason,$fosterage_matters_need_attention,$fosterage_start_time,$fosterage_end_time){
        $time = date('Y-m-d H:i:s');
        $files = Request::instance()->file('fosterage_img');
        // 移动到框架应用根目录/public/uploads/ 目录下
        $info = $files->move(ROOT_PATH . 'public' . DS. 'uploads'.DS.'fosterage_img');

        if($info){
            // 成功上传后 获取上传信息
            // 输出 jpg
            $path = '/uploads/fosterage_img/' . $info->getSaveName();
            $path = str_replace('\\', '/', $path);

            $data = ['user_id' =>$user_id,                                          'be_fosterage_user_id'=>$be_fosterage_user_id,
                'fosterage_name' =>$fosterage_name,
                'fosterage_phone' =>$fosterage_phone,
                'fosterage_reason' =>$fosterage_reason,
                'fosterage_matters_need_attention' => $fosterage_matters_need_attention,
                'fosterage_start_time' =>$fosterage_start_time,
                'fosterage_end_time' =>$fosterage_end_time];

            $fosterage_id = Db::table('ym_fosterage')->insertGetId($data);

            $img_data = ['fosterage_img_id' => null,'fosterage_id' =>$fosterage_id,'fosterage_img' =>$path];
            Db::table('ym_fosterage_img')->insert($img_data);

            $message_data = ['be_user_id'=>$be_fosterage_user_id,
                'user_id'=>$user_id,'message_type'=>6,
                'create_time'=>$time];
            $m_id = Db::table('ym_message')->insertGetId($message_data);
            $fosterage_data = ['m_id'=>$m_id,'fosterage_id'=>$fosterage_id];
             Db::table('ym_message_fosterage')->insertGetId($fosterage_data);

            $user_name = Db::query("select user_name from user_info  where user_id = '$user_id'");
            $name = $user_name[0]['user_name'];

            // $message = '关注了你';
            $be_fosterage_user_id = strval($be_fosterage_user_id);
            $this->push($be_fosterage_user_id,$name.'想要将他的宠物寄养给你');
            return $this->returnmsg(200,'寄养表填写成功');
        }
    }



    //显示寄样表信息
    public function show_fosterage_form($fosterage_id){
        $result = Fo::getFosterageForm($fosterage_id);
        return $this->returnmsg(200,"寄养表信息",$result);
    }


    //同意或者不同意寄养
    public function fosterage_agree_or_refuse($fosterage_agree,$reason,$user_id,$fosterage_id){
        $time = date('Y-m-d H:i:s');
        $be_id = Db::query("select user_id from ym_fosterage where fosterage_id = '$fosterage_id'");
        $be_user_id = $be_id[0]['user_id'];
        $user_name = Db::query("select user_name from user_info  where user_id = '$user_id'");
        $name = $user_name[0]['user_name'];
        if ($fosterage_agree == 0) {
            //拒绝收养
            $refuse_data = ['fosterage_id'=>$fosterage_id,'user_id'=>$user_id,'fosterage_agree'=>$fosterage_agree,'fosterage_refuse_reason'=>$reason];
            Db::table('ym_fosterage_agree_or_refuse')->insert($refuse_data);
            Db::query("update ym_fosterage set fosterage_status = 0 where fosterage_id = '$fosterage_id'");
            //message
            $message_data = ['be_user_id'=>$be_user_id,'user_id'=>$user_id,'message_type'=>7,'create_time'=>$time];
            $m_id = Db::table('ym_message')->insertGetId($message_data);
            $fosterage_data = ['m_id'=>$m_id,'refuse_reason'=>$reason,'fosterage_id'=>$fosterage_id];
             Db::table('ym_message_fosterage_agree_or_not')->insertGetId($fosterage_data);

            $be_user_id = strval($be_user_id);
            $this->push($be_user_id,$name.'拒绝收养的你宠物,原因:'.$reason);
            return $this->returnmsg(200,"拒绝收养",$refuse_data);
        }else{
            $agree_data = ['fosterage_id'=>$fosterage_id,'user_id'=>$user_id,'fosterage_agree'=>$fosterage_agree];
            Db::table('ym_fosterage_agree_or_refuse')->insert($agree_data);

            Db::query("update ym_fosterage set fosterage_status = 1 where fosterage_id = '$fosterage_id'");

            //message
            $message_data = ['be_user_id'=>$be_user_id,'user_id'=>$user_id,'message_type'=>7,'create_time'=>$time];
            $m_id = Db::table('ym_message')->insertGetId($message_data);
            $fosterage_data = ['m_id'=>$m_id,'fosterage_id'=>$fosterage_id];
            $m_fosterage_id = Db::table('ym_message_fosterage_agree_or_not')->insertGetId($fosterage_data);

            $be_user_id = strval($be_user_id);
            $this->push($be_user_id,$name.'同意收养的你宠物');
            return $this->returnmsg(200,"同意收养",$agree_data);
        }
    }



//用户是否同意接受寄养
    public function fosterage_or_not($user_id,$fosterage_or_not){
        if ($fosterage_or_not == 1) {
            Db::query("update user_info set fosterage_or_not = 1 where user_id = '$user_id'");
            return $this->returnmsg(200,'已开启');
        }else{
            Db::query("update user_info set fosterage_or_not = 0 where user_id = '$user_id'");
            return $this->returnmsg(200,'已关闭');
        }
    }



//返回是否同意寄养json
    public function judgement_fosterage($user_id){
        $result = Db::query("select fosterage_or_not from user_info where user_id = '$user_id'");
        return json($result[0]);
    }













//推送全部
    public function push($user_id,$message){
        $appkey = 'a3d1bad973276db7cee3f40a';
        $master_secret = 'ac5c6c6b1101643082c88913';
        $client = new JPush($appkey,$master_secret);
        $r_id = Db::query("select registration_id from ym_device_no where user_id = '$user_id'");
        $registration_id = $r_id[0]['registration_id'];

        $push = $client->push();
        $push->setPlatform('all');
        //别名
        $push->addAlias($user_id);
        //设备号
        $push->addRegistrationId($registration_id);
        // $push->iosNotification();
        $push->iosNotification($message, [
            'sound' => 'sound.cat',
            'badge' => '+1',

        ]);
        $push->options(array(
            "apns_production" => true
        ));
        // try {
        $push->send();

    }


}